package com.zut.demo1.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Access_Token {
    public BufferedReader PostMain() {
        try {
//首先需要获取token，是根据key和secret生成的
            URL targetUrl = new URL("http://10.10.31.200:8863/open_api/authentication/get_access_token");
//创建一个POST请求，调用上面的API URL信息
            HttpURLConnection httpConnection = (HttpURLConnection) targetUrl.openConnection();
            httpConnection.setDoOutput(true);
            httpConnection.setRequestMethod("POST");
            httpConnection.setRequestProperty("Content-Type", "application/json");
//构建一个json对象，key,secret的值在应用查看中获取
            String input = "{\"key\":202011122021212129,\"secret\":\"4153747cb2b0c16eddd8a5aa5196bfce3da74536\"}";
//将请求返回的结果写入流中
            OutputStream outputStream = httpConnection.getOutputStream();
            outputStream.write(input.getBytes());
            outputStream.flush();

            if (httpConnection.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : " +
                        httpConnection.getResponseCode());
            }
//读取流信息，答应出返回的结果
            BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
                    (httpConnection.getInputStream())));

            return responseBuffer;
//断开HTTP连接
//            httpConnection.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public BufferedReader getDatas(String access_token,String teaID) {
        try {
            URL targetUrl = new URL("http://10.10.31.200:8863/open_api/customization/vjzgall/full?access_token="+access_token+"&ZGH="+teaID);
            HttpURLConnection httpConnection = (HttpURLConnection) targetUrl.openConnection();
            httpConnection.setDoOutput(true);
            httpConnection.setRequestMethod("POST");
            httpConnection.setRequestProperty("Content-Type", "application/json");
            String input = "{\"key\":202011122021212129,\"secret\":\"4153747cb2b0c16eddd8a5aa5196bfce3da74536\"}";
            OutputStream outputStream = httpConnection.getOutputStream();
            outputStream.write(input.getBytes());
            outputStream.flush();

            if (httpConnection.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : " +
                        httpConnection.getResponseCode());
            }
            BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
                    (httpConnection.getInputStream())));

            return responseBuffer;
//            httpConnection.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
